package com.amat0urheu.service.impl;

import com.amat0urheu.dao.CaseMapper;
import com.amat0urheu.dao.CloudMapper;
import com.amat0urheu.pojo.*;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.service.CaseService;
import com.amat0urheu.util.CommonUtil;
import com.amat0urheu.util.generator.SnowFlakeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class CaseServiceImpl implements CaseService {
    @Value("${marsctf.uploadPath}")
    private String uploadPath;

    @Value("${marsctf.imagePath}")
    private String imagePath;

    @Value("${marsctf.imageUrl}")
    private String imageUrl;
    @Autowired
    CaseMapper caseMapper;
    @Override
    public ReturnRes getAllCaseTypes(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<CaseType> caseTypeList = caseMapper.getAllCaseTypes();

        if (caseTypeList != null) {
            return new ReturnRes(true, new PageInfo<>(caseTypeList), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes updateCaseType(CaseType caseType) {
        int i = caseMapper.updateCaseType(caseType);

        if (i>0){
            return new ReturnRes(true, "更新成功");
        }
        return new ReturnRes(false, "更新失败");
    }

    @Override
    public ReturnRes saveCaseType(CaseType caseType) {
        System.out.println(caseType);
        int i =  caseMapper.saveCaseType(caseType);
        if (i>0){
            return new ReturnRes(true, "增加成功");
        }
        return new ReturnRes(false, "增加失败");
    }

    @Override
    public ReturnRes removeCaseType(Integer id) {
        int i =  caseMapper.removeCaseType(id);
        if (i>0){
            return new ReturnRes(true, "删除成功");
        }
        return new ReturnRes(false, "删除失败");

    }

    @Override
    public ReturnRes getAllCases(int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Case> caseList = caseMapper.getAllCases();

        if (caseList != null) {
            return new ReturnRes(true, new PageInfo<>(caseList), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }


    @Override
    public ReturnRes removeCase(Integer id) {

        int i =  caseMapper.removeCase(id);
        caseMapper.removeCaseTypeIdofCase(id);
        if (i>0){
            return new ReturnRes(true, "删除成功");
        }

        return new ReturnRes(false, "删除失败");
    }
    @Override
    public ReturnRes saveCase(Case acase) {
        int i =  caseMapper.saveCase(acase);
        System.out.println(acase);
        int count=0;
        for (int j = 0; j < acase.getCaseTypeId().length; j++) {
            int a=caseMapper.saveCaseAndType(acase.getId(),acase.getCaseTypeId()[j]);
            count +=a;
        }

        if (i>0){
            return new ReturnRes(true, "添加成功");
        }
        return new ReturnRes(false, "添加失败");
    }

    @Override
    public ReturnRes updateCase(Case aCase ,MultipartFile file,String oldPath) {
        File dest = new File(oldPath);
        if (dest.exists()){
            if( dest.delete()){
                System.out.println("图片更换成功");
            }
        }
        try {

            file.transferTo(new File(imagePath+imageUrl+aCase.getCasePicture()));
            aCase.setCasePicture(imageUrl+aCase.getCasePicture());
        }catch (Exception e){
            e.printStackTrace();
            return new ReturnRes(false, "更新失败");
        }
        int i =  caseMapper.updateCase(aCase);
        caseMapper.removeCaseTypeIdofCase(aCase.getId());
        int count=0;
        System.out.println(aCase.getCaseTypeId());
        for (int j = 0; j < aCase.getCaseTypeId().length; j++) {
            int a=caseMapper.saveCaseAndType(aCase.getId(),aCase.getCaseTypeId()[j]);
            count +=a;
        }
        if (i>0){
            return new ReturnRes(true, "更新成功");
        }
        return new ReturnRes(false, "更新失败");
    }

    @Override
    public ReturnRes getCaseById(Integer id) {
        Case aCase=   caseMapper.getCaseById(id);
        Integer[] caseTypeIds=caseMapper.getCaseTypeIdsOfCase2(id);
        aCase.setCaseTypeId(caseTypeIds);
        if (aCase != null) {
            return new ReturnRes(true, aCase, "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes uploadImageForCase(MultipartFile file, Integer eid) {
      /*  if (file.isEmpty()) {
            return new ReturnRes(false, "上传失败，请选择文件");
        }
        String fileName = file.getOriginalFilename();
        int lastIndexOf = fileName.lastIndexOf(".");
        String ext = lastIndexOf != -1 ? fileName.substring(lastIndexOf + 1) : "";
        String newName = String.valueOf(SnowFlakeUtil.generatorUid()) + "." + ext;
        File dest = new File(imagePath + imageUrl, newName);
        try {
            ExperimentImage exImage = new ExperimentImage();
            exImage.setEid(eid);
            exImage.setFpath(newName);
            expeImageService.save(exImage);
            file.transferTo(dest);  // 上传成功


            return new ReturnRes(true, imageUrl + newName, "图片上传成功");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        return new ReturnRes(false, "上传失败，服务器错误");
    }

    @Override
    public ReturnRes searchCaseTypeForAdmin(String key, String value, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        String[] whiteList = new String[]{"caseType"};
        if (CommonUtil.strArrayIsHave(whiteList, key)) {
            List<CaseType>  experimentList = caseMapper.searchCaseType(key, "%" + value + "%");
            if (experimentList != null) {
                return new ReturnRes(true, new PageInfo<>(experimentList), "查询成功");
            } else {
                return new ReturnRes(false, "查询失败");
            }
        } else {
            return new ReturnRes(false, "师傅请勿尝试不安全的参数");
        }

    }

    @Override
    public ReturnRes searchCaseForAdmin(String key, String value, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String[] whiteList = new String[]{"caseName"};
        if (CommonUtil.strArrayIsHave(whiteList, key)) {
            List<Case>  experimentList = caseMapper.searchCases(key, "%" + value + "%");
            if (experimentList != null) {
                return new ReturnRes(true, new PageInfo<>(experimentList), "查询成功");
            } else {
                return new ReturnRes(false, "查询失败");
            }
        } else {
            return new ReturnRes(false, "师傅请勿尝试不安全的参数");
        }
    }

    @Override
    public ReturnRes getAllCasesBytype(int pageSize, int pageNum, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        List<Case> caseList = caseMapper.getAllCasesByType(type);
        if (caseList != null) {
            return new ReturnRes(true, new PageInfo<>(caseList), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes updateCaseNoimage(Case aCase) {

        int i = caseMapper.updateCaseNoImage(aCase);
        caseMapper.removeCaseTypeIdofCase(aCase.getId());
        int count=0;
        for (int j = 0; j < aCase.getCaseTypeId().length; j++) {
            int a=caseMapper.saveCaseAndType(aCase.getId(),aCase.getCaseTypeId()[j]);
            count +=a;
        }
        if(i>=1){
            return new ReturnRes(true, "更新成功");
        }
        return new ReturnRes(false, "更新失败");
    }
}
